package com.lmonkey.servlet.my;

import com.lmonkey.dao.Basedao;
import com.lmonkey.servic.USERDao;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

@WebServlet("/shangc/domimacheck")
public class DoMimaCheck extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //设置字符集
        request.setCharacterEncoding("UTF-8");
        response.setContentType("text/html;charset=utf-8");

        //获取参数
        String name = request.getParameter("name");
        String pass = request.getParameter("pass");
        String newpass = request.getParameter("newpass");
        String flag = request.getParameter("flag");

        //判断密码是否正确
        int count1 = 0;
        count1 = USERDao.selectLogin(name,pass);
        if (count1>0){
            //修改密码
            int count = 0;
            count = USERDao.updateMima(newpass,name);

            if (count>0){

                if (flag != null){
                    PrintWriter out = response.getWriter();
                    out.println("<script>alert('修改成功');location.href='http://localhost:8080/lyq/admin/admin_index.jsp'</script>");
                    out.close();

                }else {
                    response.sendRedirect("mygrxxselect");
                }

            }else{
                PrintWriter out = response.getWriter();
                if (flag != null){
                    out.println("<script>alert('修改失败');location.href='http://localhost:8080/lyq/admin/admin_xuigai.jsp';</script>");
                }else{
                    out.println("<script>alert('修改失败');location.href='remimaselect';</script>");
                }

                out.close();;
            }
        }else {
            PrintWriter out = response.getWriter();
            if (flag != null){
                out.println("<script>alert('原密码输入有误');location.href='http://localhost:8080/lyq/admin/admin_xuigai.jsp';</script>");
            }else {
                out.println("<script>alert('原密码输入有误');location.href='remimaselect';</script>");
            }
            out.close();
        }

    }
}
